package com.neudu.yyzx_service.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yyzx_service.common.JSONReturn;
import com.neudu.yyzx_service.pojo.Nurserecord;
import com.neudu.yyzx_service.service.NurserecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class NurseRecordController {
    @Autowired
    private NurserecordService nurserecordService;
    @Autowired
    private JSONReturn jsonReturn;

    @RequestMapping("get_all_records")
    public String getAll(@RequestBody PageInfo pageInfo){
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            List<Nurserecord> list = nurserecordService.selectAllrecords();
            PageInfo<Nurserecord> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_record_by")
    public String getByCondition(@RequestBody Nurserecord nurserecord){
        try {
            List<Nurserecord> list = nurserecordService.selectrecordby(nurserecord);
            return jsonReturn.returnSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("add_one_record")
    public String addOne(@RequestBody Nurserecord Nurserecord){
        try {
            nurserecordService.addOne(Nurserecord);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("getrecord_by_id")
    public String getById(@RequestParam("nurserecordid") Integer nurserecordid){
        try {
            Nurserecord nurserecord = nurserecordService.getById(nurserecordid);
            return jsonReturn.returnSuccess(nurserecord);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modifyrecord_by_id")
    public String modifyById(@RequestBody Nurserecord nurserecord){
        try {
            nurserecordService.updateOne(nurserecord);
            return jsonReturn.returnSuccess(nurserecord);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("removerecord_by_id")
    public String removeByid(@RequestParam("nurserecordid") Integer nurserecordid){
        try {
            nurserecordService.removeById(nurserecordid);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("/get_records_by_nursingname")
    public String getRecordsByNursingName(@RequestParam("nursingName") String nursingName) {
        try {
            List<Nurserecord> records = nurserecordService.getRecordsByNursingName(nursingName);
            return jsonReturn.returnSuccess(records);
        } catch (Exception e) {
            return jsonReturn.returnError(e.getMessage());
        }
    }
}
